﻿using Engine.Graphics;
using ForestBorder;
using SharpDX;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Forms;

namespace WorldRacing2
{
    [Export(typeof(ICreateForestBorderMesh))]
    public class ForestBorderAddinAddon : ICreateForestBorderMesh
    {
        #region Public Methods

        public List<IGameVertex> CreateGameVerticesForForest(Vector3[] middle, float widthPointOne, float widthPointTwo, float textureScale,
             float textureUcoordinatesData, float[] middleLineDistances, float lengthOfStreet, DataForForestvertexCalclation vertexData)
        {
            try
            {
                ForestBorderData forestBorderData = (ForestBorderData)vertexData.gameDllData;

                return WorldRacing2.StreetAddin.ForestBorderAddon.CreateGameVerticesForForest(middle, widthPointOne, widthPointTwo, textureScale,
                textureUcoordinatesData, middleLineDistances, lengthOfStreet, forestBorderData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string GetGameName()
        {
            return "World Racing 2";
        }

        public DataForForestvertexCalclation GetStandardVertexData()
        {
            return new DataForForestvertexCalclation()
            {
                version = 1,
                gameDllData = new ForestBorderData()
                {
                    vertexColor = new Color4()
                    {
                        Alpha = 1.0f,
                        Blue = 0.2f,
                        Green = 0.3f,
                        Red = 0.5f
                    },

                    diffuseColor = new Color4()
                    {
                        Red = 0.4f,
                        Green = 0.4f,
                        Blue = 0.4f,
                        Alpha = 0.4f
                    }
                }
            };
        }

        public DataForForestvertexCalclation ReadForestAddinData(BinaryReader br, int Version)
        {
            try
            {
                switch (Version)
                {
                    case 0:
                        return new DataForForestvertexCalclation()
                        {
                            version = 1,
                            gameDllData = new ForestBorderData()
                            {
                                vertexColor = new Color4()
                                {
                                    Alpha = 1.0f,
                                    Blue = 0.2f,
                                    Green = 0.3f,
                                    Red = 0.5f
                                },

                                diffuseColor = new Color4()
                                {
                                    Red = 0.4f,
                                    Green = 0.4f,
                                    Blue = 0.4f,
                                    Alpha = 0.4f
                                }
                            }
                        };

                    case 1:

                        return new DataForForestvertexCalclation()
                        {
                            version = 1,
                            gameDllData = ForestBorderData.FromStream(br, Version)
                        };

                    default:
                        throw new Exception("Unsupported Forestborder data\nVersion: " + Version.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void SaveForestData(DataForForestvertexCalclation data, BinaryWriter bw)
        {
            try
            {
                ForestBorderData forestBorderData = (ForestBorderData)data.gameDllData;
                forestBorderData.ToStream(bw);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void SayHello(string tach)
        {
            MessageBox.Show(tach);
        }

        #endregion Public Methods

        #region Internal Structs

        internal struct ForestBorderData
        {
            #region Public Fields

            public Color4 vertexColor;

            #endregion Public Fields

            #region Internal Fields

            internal Color4 diffuseColor;

            #endregion Internal Fields

            #region Private Fields

            private int version;

            #endregion Private Fields

            #region Internal Methods

            internal static ForestBorderData FromStream(BinaryReader br, int version)
            {
                int versiond = br.ReadInt32();
                switch (versiond)
                {
                    case 1:
                        return LoadV1(br);

                    default:
                        throw new Exception("unsupported Verison");
                }
            }

            internal void ToStream(BinaryWriter bw)
            {
                bw.Write(this.version);
                bw.Write(this.vertexColor.Alpha);
                bw.Write(this.vertexColor.Red);
                bw.Write(this.vertexColor.Green);
                bw.Write(this.vertexColor.Blue);

                bw.Write(this.diffuseColor.Alpha);
                bw.Write(this.diffuseColor.Red);
                bw.Write(this.diffuseColor.Green);
                bw.Write(this.diffuseColor.Blue);
            }

            #endregion Internal Methods

            #region Private Methods

            private static ForestBorderData LoadV1(BinaryReader br)
            {
                return new ForestBorderData()
                {
                    version = 1,
                    vertexColor = new Color4()
                    {
                        Alpha = br.ReadSingle(),
                        Red = br.ReadSingle(),
                        Green = br.ReadSingle(),
                        Blue = br.ReadSingle()
                    },
                    diffuseColor = new Color4()
                    {
                        Alpha = br.ReadSingle(),
                        Red = br.ReadSingle(),
                        Green = br.ReadSingle(),
                        Blue = br.ReadSingle()
                    }
                };
            }

            #endregion Private Methods
        }

        #endregion Internal Structs
    }
}